﻿using Bitcoin.Cryptography;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bitcoin.Examples
{
    /// <summary>
    /// Generate a new private key, and then print it plus the corresponding address.
    /// </summary>
    /// <remarks>
    /// Can be used to create an address (and key) to send coins to and use with PrivateKeys example.
    /// </remarks>
    public static class NewKey
    {
        public static void Run(string[] args)
        {
            var testNet = args.Length > 0 &&
                          string.Equals(args[0], "testnet", StringComparison.InvariantCultureIgnoreCase);
            var parameters = testNet ? ChainParameters.TestNet() : ChainParameters.ProdNet();

            // Create new key (with new private)
            KeyPair key = KeyPair.NewKey();

            var privateKeyBytes = key.GetPrivKeyBytes();
            var dumpedPrivateKey = new BitcoinSecret(parameters, privateKeyBytes);

            var address = new BitcoinAddress(parameters, key.PubKeyHash);

            Console.WriteLine("Private key is: " + dumpedPrivateKey.ToString());
            Console.WriteLine("Address from private key is: " + address);
        }
    }
}
